package com.pdg.mcplugin.ranger.conversations;

import org.bukkit.conversations.ConversationContext;
import org.bukkit.conversations.Prompt;
import com.pdg.mcplugin.common.baseclasses.StringPromptBase;
import com.pdg.mcplugin.ranger.Ranger;
import com.pdg.mcplugin.ranger.dataproviders.PlaceNameKey;
import com.pdg.mcplugin.ranger.dataproviders.PlaceNameLocation;
import com.pdg.mcplugin.ranger.dataproviders.PlaceNameTableProvider;
import com.pdg.mcplugin.ranger.dataproviders.PublicPlaceNameKey;
import com.pdg.mcplugin.ranger.dataproviders.PublicPlaceNameTableProvider;

public class RenamePrompt extends StringPromptBase<Ranger>{

	private static final String PROMPT = "What is the new name for place named '%s'?";
	public static final Object KEY_PUBLIC = "public";
	public static final Object KEY_PLACENAME = "placename";
	public static final Object KEY_NEWNAME = "newname";
	public static final Object KEY_PLAYER = "player";
	public static final Object KEY_WORLD = "world";

	public RenamePrompt(Ranger thePlugin) {
		super(thePlugin);
	}

	@Override
	public Prompt acceptInput(ConversationContext theContext, String theInput) {
		if(theInput!=null & theInput.length()>0){
			String theNewName = theInput;
			String thePlayer = (String)theContext.getSessionData(KEY_PLAYER);
			String theWorld = (String)theContext.getSessionData(KEY_WORLD);
			String thePlaceName = (String)theContext.getSessionData(KEY_PLACENAME);
			theContext.setSessionData(KEY_NEWNAME, theNewName);
			Boolean thePublicFlag = (Boolean)theContext.getSessionData(KEY_PUBLIC);
			if(thePublicFlag){
				PublicPlaceNameKey theNewKey = new PublicPlaceNameKey(theNewName, theWorld);
				if(getPlugin().getDataProvider().getPublicPlaceNameTableProvider().exists(theNewKey)){
					return new RenamePromptConflict(getPlugin());
				}else{
					PublicPlaceNameKey theOldKey = new PublicPlaceNameKey(thePlaceName, theWorld);
					PublicPlaceNameTableProvider theTableProvider = getPlugin().getDataProvider().getPublicPlaceNameTableProvider();
					PlaceNameLocation theLocation = theTableProvider.retrieve(theOldKey);
					theTableProvider.delete(theOldKey);
					theTableProvider.create(theNewKey, theLocation);
					return new RenamePromptComplete(getPlugin());
				}
			}else{
				PlaceNameKey theNewKey = new PlaceNameKey(thePlayer, theNewName, theWorld);
				if(getPlugin().getDataProvider().getPlaceNameTableProvider().exists(theNewKey)){
					return new RenamePromptConflict(getPlugin());
				}else{
					PlaceNameKey theOldKey = new PlaceNameKey(thePlayer, thePlaceName, theWorld);
					PlaceNameTableProvider theTableProvider = getPlugin().getDataProvider().getPlaceNameTableProvider();
					PlaceNameLocation theLocation = theTableProvider.retrieve(theOldKey);
					theTableProvider.delete(theOldKey);
					theTableProvider.create(theNewKey, theLocation);
					return new RenamePromptComplete(getPlugin());
				}
			}
			
		}else{
			return new RenamePromptCancel(getPlugin());
		}
	}

	@Override
	public String getPromptText(ConversationContext theContext) {
		String thePlaceName = (String)theContext.getSessionData(KEY_PLACENAME);
		return String.format(PROMPT,thePlaceName);
	}

}
